//
//  ViewController.m
//  Api-汇率
//
//  Created by student on 16/3/29.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *curMoney;
@property (weak, nonatomic) IBOutlet UITextField *goalMoney;
@property (weak, nonatomic) IBOutlet UITextField *rateTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *numTextField;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)btnPressed:(id)sender {
    NSString *httpUrl = @"http://apis.baidu.com/apistore/currencyservice/currency";
    NSString *httpArg = [NSString stringWithFormat:@"fromCurrency=%@&toCurrency=%@&amount=%f",self.curMoney.text,self.goalMoney.text,[self.numTextField.text floatValue]];
   
    [self request: httpUrl withHttpArg: httpArg];
}

-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {
    [self.activityView startAnimating];
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url
                                                               cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"027c141dc416ca3d5165488de2102049" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               NSLog(@"%@",response);
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                                   [self.activityView stopAnimating];
                               }
                               else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                                   NSDictionary *dict1 = dict[@"retData"];
                                   self.rateTextFiled.text = [NSString stringWithFormat:@"%@",dict1[@"convertedamount"]];
                                   [self.activityView stopAnimating];
                               }
                           }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
